package cn.stazxr.pool;

import com.alibaba.druid.pool.DruidDataSourceFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.sql.DataSource;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Properties;

/**
 * @author talentestors
 * @version 1.0
 * @since 2025.05.21
 **/
public abstract class BaseDao {

	private static final Logger log = LoggerFactory.getLogger(BaseDao.class);

	protected static DataSource DATASOURCE = null;

	static {
		Properties properties = new Properties();
		try {
			properties.load(PoolTest.class.getClassLoader().getResourceAsStream("jdbc.properties"));
		} catch (IOException e) {
			log.error(e.getMessage());
		}
		try {
			DATASOURCE = DruidDataSourceFactory.createDataSource(properties);
		} catch (Exception e) {
			log.error(e.getMessage());
		}
	}

	protected void closeAll(Connection connection, Statement statement, ResultSet resultSet) {
		if (resultSet != null) {
			try {
				resultSet.close();
			} catch (Exception e) {
				log.error(e.getMessage());
			}
		}
		if (statement != null) {
			try {
				statement.close();
			} catch (Exception e) {
				log.error(e.getMessage());
			}
		}
		if (connection != null) {
			try {
				connection.close();
			} catch (Exception e) {
				log.error(e.getMessage());
			}
		}
	}

}
